using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public enum State
{
    Idle,
    Chasing,
    Hurt,
    Attack,
    Dead
}

[RequireComponent(typeof(NavMeshAgent))]
public class Enemy : Mob
{
    [Header("Enemy Data")]
    [SerializeField]
    private float destroyTime;
    private NavMeshAgent agent;
    public GameObject target;
    private Vector3 startPos;
    private float resumeTimer;
    [SerializeField]
    private float resumeTime;
    [SerializeField]
    private float attackCD;
    private float attackTimer;

    [SerializeField]
    private float attackDistance;
    [SerializeField]
    [Tooltip("进入此距离开始攻击")]
    private float sightDistance;
    [SerializeField]
    [Tooltip("远离此距离开始脱战")]
    private float chaseDistance;
    [SerializeField]
    [Tooltip("被击退力度")]
    private float hittedForce;


    [SerializeField]
    private State state;

    protected override void Awake()
    {
        base.Awake();
        agent = GetComponent<NavMeshAgent>();
        startPos = transform.position;
    }

    private void Update()
    {
        // FindPlayer();
        switch (state)
        {
            case State.Idle:
                FindPlayer();
                break;
            case State.Chasing:
                ChasePlayer();
                break;
            case State.Hurt:
                Resume();
                break;
            case State.Attack:
                AttackPlayer();
                break;
            case State.Dead:
                target = null;
                agent.destination = gameObject.transform.position;
                agent.isStopped = true;
                break;
            default:
                break;
        }
    }

    protected override void Dead()
    {
        base.Dead();
        state = State.Dead;
        Destroy(gameObject, destroyTime);
    }

    private void Resume()
    {
        resumeTimer += Time.deltaTime;
        if (resumeTimer >= resumeTime)
        {
            animator.SetBool("Hurt", false);
            state = State.Idle;
        }
    }

    public override void TakeDamage(int damage, GameObject gObj)
    {
        if (state != State.Dead)
        {
            agent.velocity = (gameObject.transform.position - gObj.transform.position) * hittedForce;
            if (GameManager.Instance.players.Contains(gObj)) CameraManager.Instance.ShakeCamera();

            //Mob父类原代码
            base.TakeDamage(damage, gObj);

            state = State.Hurt;
            resumeTimer = 0f;
        }
    }

    protected override void SetAnimationValue(string a)
    {
        switch (a)
        {
            case "Hurt":
                animator.SetBool("Hurt", true);
                animator.SetBool("Run", false);
                break;
            case "Dead":
                animator.SetBool("Dead", true);
                break;
            default:
                break;
        }
    }

    private void ChasePlayer()
    {
        if (IsDead)
        {
            state = State.Dead;
            return;
        }
        if (Vector3.Distance(target.transform.position, transform.position) < attackDistance && !target.GetComponent<Mob>().IsDead) { state = State.Attack; return; }

        agent.isStopped = false;
        animator.SetBool("Run", true);
        if (Vector3.Distance(transform.position, target.transform.position) < chaseDistance && !target.GetComponent<Mob>().IsDead)
        {
            agent.destination = target.transform.position;
        }
        else
        {
            target = null;
            state = State.Idle;
        }
    }

    private void AttackPlayer()
    {
        if (IsDead) return;
        agent.isStopped = true;
        agent.destination = transform.position;
        if (attackTimer == attackCD)
        {
            animator.SetBool("Run", false);
            transform.LookAt(target.transform.position);
            animator.SetTrigger("Attack");
            attackTimer -= Time.deltaTime;
        }
        else if (attackTimer < 0f)
        {
            state = State.Chasing;
            attackTimer = attackCD;
        }
        else
        {
            attackTimer -= Time.deltaTime;
        }
    }

    public virtual void Attack()
    {
        if (Vector3.Distance(target.transform.position, transform.position) < attackDistance)
        {
            target.GetComponent<Mob>().TakeDamage(data.Damage, gameObject);
        }
    }

    private void FindPlayer()
    {
        //实现追击在视野内的角色
        float dis = sightDistance;
        foreach (var item in GameManager.Instance.players)
        {
            float a = Vector3.Distance(transform.position, item.transform.position);
            if (a < sightDistance && a < dis)
            {
                if (!item.GetComponent<Mob>().IsDead)
                {
                    dis = a;
                    target = item;
                }
            }
        }
        if (target != null)
        {
            state = State.Chasing;
            return;
        }
        //还原位置
        if (Vector3.Distance(transform.position, startPos) < 0.1f)
        {
            animator.SetBool("Run", false);
            agent.isStopped = true;
        }
        agent.destination = startPos;
    }

    private void OnDrawGizmos()
    {
        if (target != null)
        {
            Gizmos.color = Color.red;
            Gizmos.DrawWireSphere(target.transform.position, 3);
        }
        else
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawWireSphere(transform.position, sightDistance);
        }
    }
}